package com.genentech.bioinformatics.mobi;
/*
 * Class by Jeff Milton, except where otherwise noted.
 */
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;

import com.genentech.flexigraph.Grid;

public class TermGraph {

	private Grid tgrid = new Grid();
	private Paint mPaint = new Paint();
	private Paint nPaint = new Paint();

	public TermGraph() {
		init();
	}

	public void setGrid(Grid _tgrid) {
		tgrid = _tgrid;
	}

	public Grid getGrid() {
		return tgrid;
	}

	private void init() {
		mPaint = new Paint();
		mPaint.setDither(true);
		mPaint.setColor(Color.RED);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setStrokeWidth(2);

		nPaint = new Paint();
		nPaint.setDither(true);
		nPaint.setColor(Color.GREEN);
		nPaint.setStyle(Paint.Style.STROKE);
		nPaint.setStrokeJoin(Paint.Join.ROUND);
		nPaint.setStrokeCap(Paint.Cap.ROUND);
		nPaint.setStrokeWidth(10);

	}

	/**
	 * The termgraph knows how to draw itself.
	 * 
	 * @param _g
	 */
	public void draw(Canvas _c, Grid _grid) {
		_grid.rescale();
		tgrid.rescale();
		// int height = _grid.screenHeight(tgrid.getHeight());
		int top = _grid.Y(tgrid.getYi() + tgrid.getHeight());

		int right_pt = _grid.X(tgrid.getXi() + tgrid.getWidth());

		int yincrement = 1;
		int xincrement = 1;
		for (float y = tgrid.getymin(); y < tgrid.getymax(); y += yincrement) {
			for (float x = tgrid.getxmin(); x < tgrid.getxmax(); x += xincrement) {
				Path _path = new Path();
				_path.moveTo(_grid.X(tgrid.X(x)),
						_grid.Y(tgrid.Y(tgrid.getymin())));
				_path.lineTo(_grid.X(tgrid.X(x)),
						_grid.Y(tgrid.Y(tgrid.getymax())));
				_c.drawPath(_path, mPaint);
//				_c.drawText("" + x, tgrid.X(x), 5 + tgrid.Y(tgrid.getymin()),
//						nPaint);
			}
		}

		Rect r = new Rect(_grid.X(tgrid.getXi()), top, right_pt, _grid.Y(tgrid
				.getYi()));

		_c.drawRect(r, mPaint);
		_c.drawPoint(_grid.X(tgrid.getXi()), _grid.Y(tgrid.getYi()), nPaint);
	}

	public void setColor(int color) {
		mPaint.setColor(color);
	}

}
